﻿using System.Linq;
using Daemon.Service.Contract;
using Daemon.Model;
using System;
using System.Collections.Generic;
using Daemon.Repository.Contract;
namespace Daemon.Service
{
    public class IMOrganizationInfoService : IIMOrganizationInfoService
    {
        private readonly IIMOrganizationInfoRepository _iMOrganizationInfoRepository;

        private readonly IHMPatientDetailRepository _hMPatientDetailRepository;

        private readonly ICMPatientDetailRepository _cMPatientDetailRepository;

        public IMOrganizationInfoService(IIMOrganizationInfoRepository iMOrganizationInfoRepository, IHMPatientDetailRepository hMPatientDetailRepository, ICMPatientDetailRepository cMPatientDetailRepository)
        {
            _iMOrganizationInfoRepository = iMOrganizationInfoRepository;
            _hMPatientDetailRepository = hMPatientDetailRepository;
            _cMPatientDetailRepository = cMPatientDetailRepository;
        }

        public List<IMOrganizationInfo> GetOrganizationPatientInfosByType(int dataType)
        {
            var organizationInfos = _iMOrganizationInfoRepository.FindAll().Select(r => new IMOrganizationInfo() { OrganizeId = r.OrganizeId, OrganizeName = r.OrganizeName }).ToList();
            var (compareStartDate, compareEndDate) = GetCompareDate(dataType);
            var outPatientQty = _cMPatientDetailRepository.FindAll().Where(r => r.InTime >= compareStartDate && r.InTime <= compareEndDate).AsEnumerable().GroupBy(r => r.OrganizeId).ToDictionary(r => r.Key, r => r.Count());
            var emptyDate = new DateTime(1900, 1, 1, 0, 0, 0, 0);
            var hospitalizationQty = _hMPatientDetailRepository.FindAll().Where(r => r.InTime >= compareStartDate && (r.OutTime == emptyDate || r.OutTime > compareEndDate)).AsEnumerable().GroupBy(r => r.OrganizeId).ToDictionary(r => r.Key, r => r.Count());
            foreach (var organization in organizationInfos)
            {
                if (outPatientQty.TryGetValue(organization.OrganizeId, out int outQty))
                {
                    organization.OutPatientQty = outQty;
                }

                if (hospitalizationQty.TryGetValue(organization.OrganizeId, out int hQty))
                {
                    organization.HospitalizationQty = hQty;
                }
            }

            return organizationInfos;
        }

        private (DateTime startDate, DateTime endDate) GetCompareDate(int dataType)
        {
            DateTime compareStartDate = DateTime.Now;
            DateTime compareEndDate = DateTime.Now;
            switch (dataType)
            {
                case 1:
                    compareStartDate = compareStartDate.AddDays(1 - Convert.ToInt32(compareStartDate.DayOfWeek.ToString("d")));  //本周周一  
                    compareEndDate = compareStartDate.AddDays(6);  //本周周日  
                    break;
                case 2:
                    compareStartDate = compareStartDate.AddDays(1 - compareStartDate.Day);
                    compareEndDate = compareEndDate.AddMonths(1).AddDays(-1);  //本月月末  
                    break;
                case 3:
                    compareStartDate = new DateTime(compareStartDate.Year, 1, 1);
                    compareEndDate = new DateTime(compareEndDate.Year, 12, 31);  //本年年末  
                    break;
            }

            return (compareStartDate, compareEndDate);
        }
    }
}
